package com.vtest.quartz.task;

import com.vtest.quartz.domain.job.SysJobLog;
import com.vtest.quartz.service.SysJobService;
import com.vtest.quartz.service.impl.SysJobLogServiceImpl;
import com.vtest.quartz.service.impl.SysJobServiceImpl;
import com.vtest.quartz.service.vtest.wip.WipReportService;
import com.vtest.quartz.utils.exception.TaskException;
import com.vtest.quartz.utils.others.MailUtils;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @program: Quartz
 * @author: tiger
 * @create: 2024-12-13 10:15
 * @Description :wip报表
 */
@Component("WipReport")
public class WipReport {

    @Autowired
    private SysJobServiceImpl jobService;

    @Autowired
    private WipReportService wipService;

    /**
     * 线程本地变量
     */
    private static ThreadLocal<Integer> threadLocal = new ThreadLocal<>();

    public void test() {
        /*MailUtils.sendMail("ruhu.wang@v-test.com.cn", "ruhu.wang@v-test.com.cn"
                , "测试发送邮件", "测试发送邮件");*/
        System.out.println("test");
    }

    public void test2() {
        /*MailUtils.sendMail("ruhu.wang@v-test.com.cn", "ruhu.wang@v-test.com.cn"
                , "测试发送邮件", "测试发送邮件");*/
        System.out.println("test2");
    }


    public void sendWIPReport(String code, String prodCat, String mailTo) {
        String remoteFilePath = "\\\\192.168.10.216\\d$\\Auto_Report\\Reports";
        remoteFilePath = "\\\\192.168.10.216\\Reports";

        String latestFileName = wipService.getLatestFileName(remoteFilePath, code, prodCat);

        LocalDate date = LocalDate.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String time = date.format(dateTimeFormatter);

        File file = new File(remoteFilePath + "\\" + latestFileName);
        if (file.exists()) {
            String mailCc = "ruhu.wang@v-test.com.cn;";

            StringBuilder sb = new StringBuilder(code);
            sb.append("_");
            if (prodCat.equals("CP")) sb.append("WipAndInventoryReport_");
            if (prodCat.equals("FT")) sb.append("FinalTestWipDailyReport_");
            sb.append(time);

            String subject = sb.toString();

            String message = sb.toString();

            MailUtils.sendMail(mailTo, mailCc, subject, message, remoteFilePath + "\\" + latestFileName);
        }


    }

}
